package com.ebeitech.doorapp.push.xiaomi;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

import com.ebeitech.doorapp.application.EbeiApplication;
import com.ebeitech.doorapp.http.EbeiErrorCode;
import com.ebeitech.doorapp.http.HttpListener;
import com.ebeitech.doorapp.http.HttpService;
import com.ebeitech.doorapp.http.service.HttpCommonServiceRx;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.Log;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.TimeUtil;
import com.huawei.android.pushagent.api.PushManager;
import com.xiaomi.mipush.sdk.MiPushClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class PushUtils {

	private static boolean IS_PUSH_SUPPORT_HUAWEI = false;

	/**
	 * 启动推送服务
	 * 
	 * @param activity
	 */
	public static void startPushService(Activity activity) {
		if (IS_PUSH_SUPPORT_HUAWEI && "huawei".equalsIgnoreCase(Build.MANUFACTURER)) {
			Log.i("---启动华为推送---");
			PushManager.requestToken(activity);
		} else {//xiaomi 和其他类型的手机
			String regId = SPManager.getInstance(activity).get(SPConstants.SHARED_PRE_CHANNEL_ID_KEY, "");
			if (StringUtil.isStringNullOrEmpty(regId) || StringUtil.isStringNullOrEmpty(MiPushClient.getRegId(activity))) {
				Log.i("---启动小米推送---");
				MiPushClient.registerPush(activity, XMConstants.XM_APP_ID, XMConstants.XM_APP_KEY);
			} else {
				Log.i("---恢复小米推送---");
				MiPushClient.resumePush(activity, null);
			}
		}
	}

	/**
	 * 停止推送服务
	 * 
	 * @param activity
	 */
	public static void stopPushService(Activity activity) {
		if (IS_PUSH_SUPPORT_HUAWEI && "huawei".equalsIgnoreCase(Build.MANUFACTURER)) {
			Log.i("---停止华为推送---");
			PushManager.enableReceiveNotifyMsg(activity, false);
		} else {
			Log.i("---暂停小米推送---");
			MiPushClient.pausePush(activity, null);
//			Log.i("---停止小米推送---");
//			MiPushClient.unregisterPush(activity);
		}
	}

	/**
	 * @param state
	 * @return 获得发送我的登录状态的参数
	 */
	private static Map<String, String> sendMyStateUrlParams(String state) {
		String account = EbeiApplication.getUserAccount();
		String channelId =    SPManager.getInstance(EbeiApplication.getContext()).get(SPConstants.SHARED_PRE_CHANNEL_ID_KEY,"");
		String phoneplatform =    SPManager.getInstance(EbeiApplication.getContext()).get(SPConstants.SHARED_PRE_CHANNEL_STYLE,"");

		Map<String, String> valuePair = new HashMap<String, String>();
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("platform", phoneplatform);
			jsonObject.put("deviceid", channelId);
			jsonObject.put("dev", "0");//1开发环境，0或不设置此参数，正式环境
			jsonObject.put("time", TimeUtil.getCurrentTime());
			jsonObject.put("userid", SPManager.getInstance(EbeiApplication.getContext()).get(SPConstants.USER_ACCOUNT, ""));
			if (SPConstants.LOGIN_STATE.equals(state)) {
				jsonObject.put("bind", "1");
			} else {
				jsonObject.put("bind", "0");
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		valuePair.put("message", jsonObject.toString());
		return valuePair;
	}

	public static void sendMyStateUrl(Context context, String state) {
		Map<String, String> valuePair = PushUtils.sendMyStateUrlParams(SPConstants.LOGIN_STATE);
		HttpService mHttpService = HttpService.getInstance();
		HttpCommonServiceRx mHttpCommonServiceRx = mHttpService.getRxGsonRetrofit().create(HttpCommonServiceRx.class);
		Call<ResponseBody> call = null;
		if (SPConstants.LOGIN_STATE.equals(state)) {
			call = mHttpCommonServiceRx.doBindPush(valuePair);
		} else {
			call = mHttpCommonServiceRx.doUnBindPush(valuePair);
		}
		mHttpService.executeHttp(context, call,
				new HttpListener<String>() {
					@Override
					public void onStart() {

					}

					@Override
					public void onError(EbeiErrorCode errorCode) {

					}

					@Override
					public void onSuccess(String result) {

					}
				}, String.class);
		Log.i("pushservice","state = " + state);
	}
}
